<?php
namespace RMD\AccessBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

use Doctrine\Common\Collections\ArrayCollection;

class User implements UserInterface, \Serializable
{
	private $id;    
	private $username;
	private $password;
	private $salt;
	private $role;
	private $email;
	private $isActive;
	private $dateCreated;
	
	protected $lists;

	public function __construct() {
        $this->role = 'ROLE_USER';
        $this->isActive = false;
        $this->salt = md5(uniqid(null, true));
	$this->lists = new ArrayCollection();
    }
    
    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addConstraint(new UniqueEntity(array('fields' => array('username'))));
        $metadata->addConstraint(new UniqueEntity(array('fields' => array('email'))));
        
        $metadata->addPropertyConstraint('username', new NotBlank());
        $metadata->addPropertyConstraint('username', new Length(array("max" => 50)));   
        $metadata->addPropertyConstraint('username', new Regex(array("pattern" => '/^[A-Za-z][A-Za-z0-9_-]+/', 'message' => 'Must contain only latin letters and numbers and start with letter')));   

        $metadata->addPropertyConstraint('email', new NotBlank());
        $metadata->addPropertyConstraint('email', new Length(array("max" => 60)));
        
        $metadata->addPropertyConstraint('password', new NotBlank());
    }
    
    public function eraseCredentials() {
        ;
    }
    public function getPassword() {
        return $this->password;
    }
    public function getRoles() {
        return array($this->role);
    }
    public function getSalt() {
        return $this->salt;
    }
    public function getUsername() {
        return $this->username;
    }

    public function unserialize($serialized) {
        list($this->id) = unserialize($serialized);
    }

    public function serialize() {
        return serialize(array($this->id));
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set username
     *
     * @param string $username
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;
    
        return $this;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return User
     */
    public function setPassword($password)
    {
        $this->password = $password;
    
        return $this;
    }
    /**
     * Set role
     *
     * @param string $role
     * @return User
     */
    public function setRole($role)
    {
        $this->role = $role;
    
        return $this;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->email = $email;
    
        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set isActive
     *
     * @param boolean $isActive
     * @return User
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;
    
        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean 
     */
    public function getIsActive()
    {
        return $this->isActive;
    }

    /**
     * Set salt
     *
     * @param string $salt
     * @return User
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;
    
        return $this;
    }

    /**
     * Get role
     *
     * @return string 
     */
    public function getRole()
    {
        return $this->role;
    }

    /**
     * Set dateCreated
     *
     * @param \DateTime $dateCreated
     * @return User
     */
    public function setDateCreated($dateCreated)
    {
        $this->dateCreated = $dateCreated;
    
        return $this;
    }

    /**
     * Get dateCreated
     *
     * @return \DateTime 
     */
    public function getDateCreated()
    {
        return $this->dateCreated;
    }

    /**
     * Add lists
     *
     * @param \RMD\AccountBundle\Entity\Lists $lists
     * @return User
     */
    public function addList(\RMD\AccountBundle\Entity\Lists $lists)
    {
        $this->lists[] = $lists;
    
        return $this;
    }

    /**
     * Remove lists
     *
     * @param \RMD\AccountBundle\Entity\Lists $lists
     */
    public function removeList(\RMD\AccountBundle\Entity\Lists $lists)
    {
        $this->lists->removeElement($lists);
    }

    /**
     * Get lists
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getLists()
    {
        return $this->lists;
    }
}